<script setup>
import {ref, onMounted, nextTick, onUpdated, computed} from 'vue'
import { User, Collection, Document } from '@element-plus/icons-vue'
import {useUserStore} from "@/stores/userStores.js";
import { useTeacherStores } from '@/stores/teacherStores.js'
import {storeToRefs} from "pinia";
import axios from "axios";
import baseURL from "@/config/config.js";
const teacherStore = useTeacherStores()
const {baseInfo} = storeToRefs(useUserStore());
const { sum_stu, sum_class, sum_lectures } = storeToRefs(teacherStore)
// 教师信息
const teacherAvatar = ref('')
const currentDate = ref(new Date().toLocaleDateString())

// 最近活动
const recentActivities = ref([
  {
    content: '更新了《高等数学》课程的期末成绩',
    time: '2024-01-15 14:30',
    type: 'success'
  },
  {
    content: '发布了新的作业通知',
    time: '2024-01-14 16:00',
    type: 'info'
  },
  {
    content: '参加了教学工作例会',
    time: '2024-01-13 09:00',
    type: 'primary'
  }
])
const tableData = computed(() => {
  return logData.value
})
const logData = ref([])
const currentPage = ref(1)
const pageSize = ref(5)
const dataLength = ref(0)
const handlePageChange = (page) => {

  axios({
    url: baseURL + '/log/get-login-log-by-condition',
    params: {
      page: currentPage.value,
      size: pageSize.value
    },
    headers: {
      Authorization: 'Bearer ' + localStorage.getItem('token')
    }
  }).then(res => {
    dataLength.value = res.data.data.total
    logData.value = res.data.data.logs
    console.log('赋值后的logData:', logData.value); // 检查是否有数据
  })
  currentPage.value = page
}
onMounted(() => {
  handlePageChange(1)
})
</script>

<template>
  <div class="home-page">
    <el-row :gutter="20">
      <!-- 欢迎卡片 -->
      <el-col :span="24">
        <el-card class="welcome-card">
          <template #header>
            <div class="welcome-header">
              <el-avatar :size="64" :src="teacherAvatar" />
              <div class="welcome-info">
                <h2>欢迎回来，{{ baseInfo.name}}老师</h2>
                <p>{{ currentDate }}</p>
              </div>
            </div>
          </template>
          <div class="welcome-stats">
            <el-row :gutter="20">
              <el-col :span="8">
                <div class="stat-item">
                  <el-icon class="stat-icon"><User /></el-icon>
                  <div class="stat-info">
                    <h3>{{ sum_stu }}</h3>
                    <p>学生总数</p>
                  </div>
                </div>
              </el-col>
              <el-col :span="8">
                <div class="stat-item">
                  <el-icon class="stat-icon"><Collection /></el-icon>
                  <div class="stat-info">
                    <h3>{{ sum_class }}</h3>
                    <p>班级数量</p>
                  </div>
                </div>
              </el-col>
              <el-col :span="8">
                <div class="stat-item">
                  <el-icon class="stat-icon"><Document /></el-icon>
                  <div class="stat-info">
                    <h3>{{ sum_lectures }}</h3>
                    <p>课程数量</p>
                  </div>
                </div>
              </el-col>
            </el-row>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="mt-20">
      <!-- 最近活动 -->
      <el-col :span="8">
        <el-card class="recent-activities">
          <template #header>
            <div class="card-header">
              <h3>最近活动</h3>
            </div>
          </template>
          <el-timeline>
            <el-timeline-item
              v-for="(activity, index) in recentActivities"
              :key="index"
              :timestamp="activity.time"
              :type="activity.type"
            >
              {{ activity.content }}
            </el-timeline-item>
          </el-timeline>
        </el-card>
      </el-col>

      <!-- 待办事项 -->
      <el-col :span="12">
        <el-card class="chart-card">
          <template #header>
            <div class="chart-header">
              <span>登录日志</span>
            </div>
          </template>
          <div class="chart-container">
            <el-table :data="tableData" border                 stripe
                      highlight-current-row>
              <el-table-column prop="username" label="用户名"  align="center" />
              <el-table-column prop="date" label="时间" align="center" />
              <el-table-column prop="ip" label="IP地址"  align="center" />
              <el-table-column fixed="right" label="状态" width="107" align="center">
                <template #default="scope">
                  <el-tag :type="scope.row.status === 'in' ? 'success' : 'danger'">
                    {{ scope.row.status === 'in' ? '登录' : '登出' }}
                  </el-tag>
                </template>
              </el-table-column>
            </el-table>
            <el-pagination
                @current-change="handlePageChange"
                :current-page="currentPage"
                :page-size="pageSize"
                layout="total, prev, pager, next, jumper"
                :total="dataLength">
            </el-pagination>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped>
.home-page {
  padding: 20px;
}

.welcome-card {
  margin-bottom: 20px;
}

.welcome-header {
  display: flex;
  align-items: center;
  gap: 20px;
}

.welcome-info h2 {
  margin: 0;
  font-size: 24px;
  color: #1f2937;
}

.welcome-info p {
  margin: 5px 0 0;
  color: #6b7280;
}

.welcome-stats {
  margin-top: 20px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 20px;
  background-color: #f8fafc;
  border-radius: 8px;
}

.stat-icon {
  font-size: 32px;
  color: var(--el-color-primary);
}

.stat-info h3 {
  margin: 0;
  font-size: 24px;
  color: #1f2937;
}

.stat-info p {
  margin: 5px 0 0;
  color: #6b7280;
}

.mt-20 {
  margin-top: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.card-header h3 {
  margin: 0;
  font-size: 16px;
  color: #1f2937;
}

.todo-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid #f0f0f0;
}

.todo-item:last-child {
  border-bottom: none;
}

.todo-date {
  font-size: 12px;
  color: #6b7280;
}

:deep(.el-timeline-item__content) {
  color: #4b5563;
}

:deep(.el-checkbox__label) {
  color: #4b5563;
}
.chart-container {
  height: 50vh;
  margin: 0 24px 24px;
}
.chart-card{
  height: 65vh;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  border-radius: 16px;
  transition: transform 0.3s ease;
}
</style>